package cn.chinaunicom.fast.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;

import cn.chinaunicom.fast.common.utils.UserContextHolder;
import lombok.extern.slf4j.Slf4j;

/**
  *
  * @ProjectName:    common.web
  * @Package:        com.springboot.cloud.common.web.interceptor
  * @ClassName:      UserInterceptor
  * @Description:    用户信息拦截器 - 登录用户信息拦截器, 解析网关给传过来的登录用户信息
  * @Author:         wangjc
  * @CreateDate:     2020/3/18 0018 13:57
  * @UpdateUser:     更新者
  * @UpdateDate:     2020/3/18 0018 13:57
  * @UpdateRemark:   更新内容
  * @Version:        1.0
  */
@Slf4j
public class UserInterceptor implements HandlerInterceptor {

    private Boolean reject;

    public UserInterceptor(boolean reject) {
        this.reject = reject;
    }

    @SuppressWarnings("unchecked")
	@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return !reject;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        UserContextHolder.getInstance().clear();
    }
}
